//
//  XSButton.m
//  writemall
//
//  Created by mac on 2021/7/14.
//  Copyright © 2021 p.write.mall. All rights reserved.
//

#import "XSButton.h"

#import<objc/runtime.h>

@implementation XSButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (void)changeVerfiyButtonState {
    __block int timeout = 60; //倒计时时间
       dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
       dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
       dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
       dispatch_source_set_event_handler(timer, ^{
           if(timeout <= 0){ //倒计时结束，关闭
               dispatch_source_cancel(timer);
               dispatch_async(dispatch_get_main_queue(), ^{
                   [self setEnabled:YES];
                   [self setTitle:@"获取验证码" forState:UIControlStateNormal];
               });
           }else{
               dispatch_async(dispatch_get_main_queue(), ^{
                   [self setEnabled:NO];
                   [self setTitle:[NSString stringWithFormat:@"%ds",timeout] forState:UIControlStateNormal];
               });
               timeout--;
           }
       });
       dispatch_resume(timer);
}

- (void)addTapRunWithBlock:(YSBlockButton)block
{
    self.block = block;
    [self addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)touchAction:(XSButton *)blockButton
{
    if(self.block)
    {
        self.block(blockButton);
    }
}

- (void)layoutButtonWithEdgeInsetsStyle:(XSButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space
{
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case XSButtonEdgeInsetsStyleImageTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case XSButtonEdgeInsetsStyleImageLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case XSButtonEdgeInsetsStyleImageBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case XSButtonEdgeInsetsStyleImageRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        case XSButtonEdgeInsetsStyleImageCenter:
        {
            imageEdgeInsets = UIEdgeInsetsMake((self.frame.size.height-imageHeight)/2, (self.frame.size.width-imageWith)/2, (self.frame.size.height-imageHeight)/2, (self.frame.size.width-imageWith)/2);
        }
            break;
        default:
            break;
    }
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}
/**  快捷获取button*/
+ (XSButton *)getButtonSuperView:(id)superView rect:(CGRect)rect text:(NSString *)text textColor:(UIColor *)color font:(UIFont *)font
{
    XSButton *button=[XSButton buttonWithType:UIButtonTypeCustom];
    [(UIView *)superView addSubview:button];
    button.frame=rect;
    [button setTitle:text forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font=font;
    return button;
}
@end
